$(function () {
    getuserinfo();
    $("#loginOut").on("click", function () {
        layer.confirm('确认退出登录吗?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            //do something
            location.href = "/login.html";
            localStorage.removeItem("token");
            layer.close(index);
        });
    })
})

function getuserinfo() {
    axios({
        method: "GET",
        url: "/my/userinfo",
        headers: {
            Authorization: localStorage.getItem("token")
        }
    }).then(res => {
        if (res.data.status !== 0) {
            return layui.layer.msg(res.data.message)
        }
        useavatar(res.data.data);
    })
}

function useavatar(user) {
    let name = user.nickname || user.username;
    $(".come").html("欢迎&emsp;" + name);
    $(".name").html(name);
    if (user.user_pic !== null) {
        $(".avatar-text").hide();
        $(".layui-nav-img").show().attr("src", user.user_pic);
    } else {
        $(".layui-nav-img").hide();
        $(".avatar-text").show().html(name[0].toUpperCase());
    }
}